
 package me.exchange.com.config;

 import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
 import com.google.common.base.Predicates;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Import;
 import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
 import springfox.documentation.builders.ApiInfoBuilder;
 import springfox.documentation.builders.ParameterBuilder;
 import springfox.documentation.builders.PathSelectors;
 import springfox.documentation.schema.ModelRef;
 import springfox.documentation.service.ApiInfo;
 import springfox.documentation.service.Parameter;
 import springfox.documentation.spi.DocumentationType;
 import springfox.documentation.spring.web.plugins.Docket;
 import springfox.documentation.swagger2.annotations.EnableSwagger2;

 import java.sql.Timestamp;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;

 @Configuration
 @EnableSwagger2
 @EnableKnife4j
 @Import(BeanValidatorPluginsConfiguration.class)
 public class SwaggerConfiguration {
     /**
      * springfox.documentation.spring.web.plugins.Docket 用于构建 Springfox 框架
      * @return
      */
     @Value("${jwt.header}")
     private String tokenHeader;

     @Value("${jwt.token-start-with}")
     private String tokenStartWith;

     @Value("${knife4j.enable}")
     private Boolean enabled;

     @Bean
     @SuppressWarnings("all")
     public Docket createRestApi() {
         ParameterBuilder ticketPar = new ParameterBuilder();
         List<Parameter> pars = new ArrayList<>();
         ticketPar.name(tokenHeader).description("token")
                 .modelRef(new ModelRef("string"))
                 .parameterType("header")
                 .defaultValue(tokenStartWith + " ")
                 .required(true)
                 .build();
         pars.add(ticketPar.build());
         return new Docket(DocumentationType.SWAGGER_2)
                 .enable(enabled)
                 .apiInfo(apiInfo())
                 .select()
                 .paths(Predicates.not(PathSelectors.regex("/error.*")))
                 .build()
                 .globalOperationParameters(pars)
                 .directModelSubstitute(Timestamp.class,Date.class)
                 .ignoredParameterTypes(Timestamp.class); //添加忽略类型;
     }

     private ApiInfo apiInfo() {
         return new ApiInfoBuilder()
                 .title("eladmin 接口文档")
                 .version("2.3")
                 .build();
     }

 }

